<template>
  <el-form-item :class="getClz()" :label="opt.text" :prop="opt.name" v-if="!opt.hide"  v-bind="opt.itemProps">
    <el-select v-model="data[opt.name]" style="width: 100%;"
               :placeholder="opt.props.disabled?'':opt.placeholder"
               :clearable="opt.props.clearable"
               v-bind="opt.props"
               @change="setValueMap"
    >
      <el-option-group
        v-for="group in opt.options"
        :key="group.label"
        :label="group.label">
        <el-option
          v-for="item in group.children"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-option-group>
    </el-select>
  </el-form-item>
</template>

<script>
  import extend from '../extend';
  let Log = ()=>{};

  export default {
    name: "FormSelect",
    mixins:[extend],
    dicts:[],
    data(){
      return {
        opt: {
          name:'',
          text:'',
          prop:'',
          itemProps:{
          },
          props:{
            disabled:false,
            filterable:true,
            clearable:true
          },
          hide:false,
          placeholder:'',
          hasNull:false,
          options:[],
          optionsLabel:'label',
          optionsValue:'value',
          formatter:(val)=>{
              if(this.opt.props.multiple){
                return val || [];
              }else{
                return val + '';
              }
          }
        }
      }
    },
    mounted(){
      this.initOptions();
      this.onChange = this.opt.onChange||Log;
      this.onOptions = this.opt.onOptions||Log;
    },
    methods:{
      setValue(val = ''){
        this.data[this.opt.name] = this.opt.formatter(val);
      },
      setValueMap(val){
        let opt = this.opt, values = opt.props.multiple ? val : [val];
        this.onOptions(this.data,this.column,opt.options.filter((v)=>{
            return values.indexOf(v[opt.optionsValue]+'')>-1
        }))
        this.onChange(this.data,this.column);
      }
    }
  }
</script>

<style scoped>
</style>
